package com.twang.stock.controller.old;

import com.twang.stock.entity.*;
import com.twang.stock.mapper.StockInfoMapper;
import com.twang.stock.service.jinyan.CompanyCodeInfoService;
import com.twang.stock.service.jinyan.StockMoneyInListService;
import com.twang.stock.task.old.StockMoneyInTask;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/stock")
@Slf4j
public class StockMoneyInController {

//    @Autowired
//    private QueryDataService queryDataService;


    @Autowired
    private StockMoneyInListService stockMoneyInListService;

    @Autowired
    private CompanyCodeInfoService companyCodeInfoService;
    @Resource
    private StockInfoMapper stockInfoMapper;

    @Resource
    private StockMoneyInTask stockMoneyInTask;
    /**
     * 获取数据
     *
     * @return
     */
    @RequestMapping(value = "/moneyIn")
    public ModelAndView query(String stockName) {
        ModelAndView modelAndView = new ModelAndView();
        List list = Lists.newArrayList();
        try {
            if (StringUtils.isNotEmpty(stockName)) {
                //1、主力资金流入
                List<StockMoneyIn> stockMoneyInList = stockMoneyInListService.getMoneyInByStockName(stockName);
                if(CollectionUtils.isEmpty(stockMoneyInList)){
                    stockMoneyInTask.moneyInHistory(stockName);
                    stockMoneyInList = stockMoneyInListService.getMoneyInByStockName(stockName);
                }
                modelAndView.addObject("stockName", companyCodeInfoService.getInfoByStockName(stockName).getShortName());
                modelAndView.addObject("stockMoneyInList", stockMoneyInList);
                //2、查询股价
                List<StockInfo> stockInfoList=stockInfoMapper.biggerThanDate(stockMoneyInList.get(0).getDate(),stockName);
                modelAndView.addObject("stockInfoList", stockInfoList);
            }
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        modelAndView.setViewName("moneyIn");
        return modelAndView;
    }

}

